<?php

/**
 * Framework_Module_Sites_CP
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Modules
 * @filesource
 */


/**
 * Framework_Module_Sites_CP
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Modules
 */
class Framework_Module_Sites_CP extends Framework_Module_CP
{

	/**
     * $navParts
     *
     * @access      public
     * @var         array		$navParts      Url parts
     */
	private $navParts = array();

	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct();
		$this->tplFile = 'CpSites.tpl';
		$this->navParts = Framework::$request->requestParts;

	}

	/**
     * __default
     *
     * Generates profile form(s)
     *
     * @access 		public
     * @return 		mixed 		boolean true on success, PEAR_Error on failure
     */
	public function __default()
	{

		$siteTable = $this->owner->getTable('OwnerSites');

		$this->forms['form'] = Framework_Quickform::factory('form', 'post', Framework::$request->getBaseHTTPPath().'/'.$this->owner->userName.'/Sites/CP', null, null, true);

		$cols = array('siteName', 'URL', 'rssURL');

		$siteTable->addFormElements($this->forms['form'], $cols);

		$this->forms['form']->addElement('submit','btnSubmit','Submit','class="button" id="btnSubmit"');





		// switch to edit or remove a site
		switch(true){
			case (strcasecmp($this->navParts[0], 'edit') == 0) :
				if($this->navParts[1] && preg_match('/^[0-9]+$/i', $this->navParts[1])) {
					$siteTable->load(array('idSite' => $this->navParts[1], 'idSiteUser' => $this->owner->idSiteUser));

					$defaults = array(
					'siteName' => $siteTable->siteName,
					'URL' => $siteTable->URL,
					'rssURL' => $siteTable->rssURL
					);

					$this->forms['form']->setDefaults($defaults);
					$this->forms['form']->addElement('hidden','editID', $this->navParts[1]);
					$this->forms['form']->addElement('submit','btnCancel','Cancel','class="button" id="btnCancel"');
				}
				break;
					
			case (strcasecmp($this->navParts[0], 'delete') == 0) :
				if($this->navParts[1] && preg_match('/^[0-9]+$/i', $this->navParts[1])) {
					$where = 'idSite = '.$this->navParts[1].' AND idSiteUser = '.$this->owner->idSiteUser;
					$siteTable->delete($where);
					$this->setData('formResponse', 'Site Removed');
				}
				break;
		}





		$this->forms['form']->applyFilter('__ALL__','trim');
		$this->forms['form']->applyFilter('__ALL__','htmlentities');

		// Process form
		if ($this->forms['form']->validate()) {
				
			$result = $this->forms['form']->process(array($this, 'process'));
			if (PEAR::isError($result)) {
				$this->setData('formResponse','Error: '.$result->getMessage());

			} else {
				$this->forms['form']->getElement('siteName')->setValue('');
				$this->forms['form']->getElement('URL')->setValue('');
				$this->forms['form']->getElement('rssURL')->setValue('');
				$this->setData('formResponse',$result);
			}
		}

		// Used for Menu
		$sites = $this->owner->getSites();
		if(!PEAR::isError($sites) && count($sites)) {
			$this->setData('sites', $sites);
		}

		return true;

	}

	/**
     * process
     *
     *
     * @access 		public
     * @param 		array		$data		Data to process
     * @return 		string		String to use as formResponse
     */
	public function process($data)
	{

		if (!$data['btnCancel']) {
			$siteTable = $this->owner->getTable('OwnerSites');
			if (isset($data['editID'])) {
				$siteTable->load(array('idSite' => $data['editID'], 'idSiteUser' => $this->owner->idSiteUser));
			}
			$siteTable->idSiteUser = $this->owner->idSiteUser;
			$siteTable->siteName = $data['siteName'];
			$siteTable->URL = $data['URL'];
			$siteTable->rssURL = $data['rssURL'];

			$result = $siteTable->save();
			
			if (PEAR::isError($result)) {
				return $result;
			} else {
				return 'Submit Successful';
			}
		} else {
			return 'Edit Canceled';
		}




	}

}



?>